//
//  BaseScrollViewController.m
//  HOrangiOS
//
//  Created by YOrange on 2018/5/3.
//  Copyright © 2018年 YOrange. All rights reserved.
//

#import "BaseScrollViewController.h"
#import <UIScrollView+EmptyDataSet.h>
@interface BaseScrollViewController ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate,UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) UIScrollView *scrollView;
@end

@implementation BaseScrollViewController{
    NSInteger _indexPage;
    BOOL _isRefresh;
    BOOL _isLoadingFinish;
    //是否正在加载
    BOOL _isLoading;
    NSString *_emptyImg;
    NSString *_emptyTitle;
    UIColor *_emptyColor;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _indexPage = 1;
    // Do any additional setup after loading the view.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}

- (void)configTableView:(UITableView *)tableView nibName:(NSString *)nib identifier:(NSString *)identifier
{
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView registerNib:[UINib nibWithNibName:nib bundle:nil] forCellReuseIdentifier:identifier];
    
    
}


-(void)configEmptyView:(UITableView *)tableView img:(NSString *)img title:(NSString *)title color:(UIColor *)color{
    if (img == nil) {
        _emptyImg = @"";
    }else{
        _emptyImg = img;
    }
    if (title == nil) {
        _emptyTitle = @"暂无数据";
    }else{
        _emptyTitle = title;

    }
    if (color == nil) {
        _emptyColor = [UIColor colorWithRed:237.0 / 255.0 green:237.0 / 255.0 blue:237.0 / 255.0 alpha:1.0];
    }else{
        _emptyColor = color;
    }
    tableView.emptyDataSetSource = self;
    tableView.emptyDataSetDelegate = self;
}


#pragma mark - 刷新问题
-(void)initMJRefreshScrollView:(UIScrollView *)scrollView{
    _scrollView = scrollView;
    [self initMJRefreshHeadView:scrollView];
    [self initMJRefreshFootView:scrollView];
}

//定义上拉刷新
-(void)initMJRefreshHeadView:(UIScrollView *)scrollView{
    _scrollView = scrollView;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    header.automaticallyChangeAlpha = YES;
    header.lastUpdatedTimeLabel.hidden = YES;
    scrollView.mj_header = header;
}

//定义下拉加载
-(void)initMJRefreshFootView:(UIScrollView *)scrollView{
    _scrollView = scrollView;
    scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

    //scrollView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];

}

-(void)refresh{
    _indexPage = 1;
    _isRefresh = YES;
    [self requestData];
}

-(void)loadMore{
    if (_isLoadingFinish) {
        [self.scrollView.mj_footer endRefreshing];
        return;
    }
    _indexPage++ ;
    [self requestData];
}


#pragma mark - 网络请求
///网络请求
-(void)requestData{
    _isLoading = YES;
};



///数据
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(void)autoSuccessData:(NSDictionary *)dict{
    //加载完成
    _isLoading = NO;
    //刷新完成
    if (_indexPage == 1) {
        [_scrollView.mj_header endRefreshing];
    }else{
        [_scrollView.mj_footer endRefreshing];
    }
    //判断数据的类型看有没有问题
    id value = [dict objectForKey:@"data"];
    if (![value isKindOfClass:[NSArray class]] && ![value isKindOfClass:[NSDictionary class]]) {
        [self mbShowToast:@"数据异常"];
        return;
    }
    //隐藏loading
    [self mbDismiss];
    //判断是否刷新，是刷新的话删除原数据
    if (_isRefresh) {
        [self.dataArray removeAllObjects];
        _isRefresh = NO;
    }
    NSDictionary *dataDic = dict[@"data"];
    //解析下面的字段，根据具体情况而定
    if ([dataDic.allKeys containsObject:@"dataList"]) {
        NSArray * array=[dataDic objectForKey:@"dataList"];
        //检验数据是否加载完成
        //看后台的逻辑，1.数据总条数 2.看返回的条数和自己请求返回的数据是不一样，一般10条
        [self autoCheckDataLoadFinish:dataDic[@"rowCount"] withDataArray:array];
        [self autoSuccessDataFinish:dataDic withDataArray:array];
        
    }
}

//返回的数据，第一个为原始数据字典，第二个位数组直接用
-(void)autoSuccessDataFinish:(NSDictionary *)dict withDataArray:(NSArray*)array{
    
}

//判断数据是否加载完成。
-(void)autoCheckDataLoadFinish:(NSString *)rowCount withDataArray:(NSArray *)array{
    if (rowCount.intValue == self.dataArray.count + array.count) {
        _isLoadingFinish=YES;
    }else{
        _indexPage++;
    }
}


#pragma mark - DZNEmpty
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:_emptyImg];
}

//空白页标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = _emptyTitle;
    
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor darkGrayColor]};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


// 返回空白区域的颜色自定义
- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
    return _emptyColor;
}

-(BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView{
    return !_isLoading;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
